// packageE/healthy/healthyPaper/healthyPaper.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionList: [],
    answerList: [],
    questionType: [
      "healthySelect",
      "healthySex",
      "healthyAge",
      "healthyHeight",
      "healthyHeight",
      "healthyHeight",
      "healthyHeight",
    ],
    health_id: 0,
    currentQuestionIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.health_id);
    this.data.health_id = options.health_id;
    let scene = decodeURIComponent(options.scene);
    if (scene) {
      var info_arr = [];
      info_arr = scene.split(",");
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        console.log(chil_arr);
        if (chil_arr[0] == "health_id") {
          this.data.health_id = chil_arr[1];
        }
      }
      console.log(info_arr);
    }
    this.getQualification(this.data.health_id);
    this.getPaperData(this.data.health_id);
  },

  getQualification(health_id) {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getQualification"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        health_id,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 500);

          return;
        }
      },
    });
  },

  /* *
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  sendPost() {
    if (!this.nextQuestion(false)) return;

    console.log(this.data.answerList);
    let { health_id, answerList } = this.data;
    let sendObj = {};
    sendObj.art = "getRecommend";
    let answer_data = {};
    answer_data.health_id = health_id;
    let basic = [];
    let evaluation = [];
    for (let i = 0; i < answerList.length; i++) {
      if (answerList[i].type != 0) {
        basic.push(answerList[i]);
      } else if (answerList[i].type == 0) {
        evaluation.push(answerList[i]);
      }
    }
    answer_data.basic = basic;
    answer_data.evaluation = evaluation;
    sendObj.answer_data = answer_data;
    console.log(sendObj);
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: true,
      data: sendObj,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          return;
        }
        wx.redirectTo({
          url: "/packageE/healthy/healthyResult/healthyResult",
        });
        console.log("提交成功", resdata);
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  nextQuestion(nextFlag = true) {
    let { questionList, answerList, currentQuestionIndex } = this.data;
    if (
      questionList[currentQuestionIndex].is_require == 1 &&
      questionList[currentQuestionIndex].type != 0
    ) {
      if (
        answerList[currentQuestionIndex].answer == null ||
        answerList[currentQuestionIndex].answer == ""
      ) {
        wx.showToast({
          icon: "none",
          title: "该题为必填题",
          duration: 1500,
        });
        return false;
      }
    } else if (
      questionList[currentQuestionIndex].is_require == 1 &&
      questionList[currentQuestionIndex].type == 0
    ) {
      if (answerList[currentQuestionIndex].option.length == 0) {
        wx.showToast({
          icon: "none",
          title: "该题为必填题",
          duration: 1500,
        });
        return false;
      }
    }
    if (!nextFlag) return true;
    this.setData({ currentQuestionIndex: currentQuestionIndex + 1 });
  },
  prveQuestion() {
    this.setData({ currentQuestionIndex: this.data.currentQuestionIndex - 1 });
  },
  getPaperData(health_id) {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: true,
      data: {
        health_id,
        art: "getEvaluation",
      },
      success: (resdata) => {
        //console.log();
        var res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          return;
        }
        let basic_question = res.data.getEvaluation.basic_question;
        let evaluation_question = res.data.getEvaluation.evaluation_question;
        let answerList = [];
        console.log(basic_question, evaluation_question);
        for (let i = 0; i < basic_question.length; i++) {
          let obj = {};
          obj.basic_id = basic_question[i].basic_id;
          obj.name = basic_question[i].basic_name;
          obj.type = basic_question[i].type;
          obj.company_name = basic_question[i].company_name;
          obj.answer = null;
          answerList.push(obj);
        }

        for (let i = 0; i < evaluation_question.length; i++) {
          evaluation_question[i].type = 0;
          let obj = {};
          obj.evaluation_id = evaluation_question[i].evaluation_id;
          obj.identification = evaluation_question[i].identification;
          obj.type = 0;
          obj.option = [];
          answerList.push(obj);
        }
        let questionList = basic_question.concat(evaluation_question);
        console.log(questionList, answerList);
        this.setData({ questionList, answerList });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  quesCallback(obj) {
    let res = obj.detail;
    console.log(res);
    if (res.question.type == 0) {
      let key = "answerList[" + res.site + "].option";
      this.setData({
        [key]: res.answer,
      });
    } else {
      let key = "answerList[" + res.site + "].answer";
      this.setData({
        [key]: res.answer,
      });
      //this.data.answerList[res.site].answer = res.answer;
    }
  },
});
